﻿#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QSettings>
#include <QMainWindow>
#include <QScrollArea>
#include <QSpinBox>
#include <QKeyEvent>
#include <QLabel>
#include <QTimer>
#include <QLibrary>

#include "AboutDialog.h"
#include "MeasureView/ToolBarMeasure.h"
#include "MeasureView/ToolBarZoom.h"
#include "MeasureView/ToolBarProperty.h"
#include "MeasureView/ToolBarSemiAutoMeasure.h"
#include "MeasureView/SemiAutoMeasure/SemiAutoMeasure.h"
#include "MeasureView/SemiAutoMeasure/DialogSemiAutoMeasure.h"
#include "MeasureView/MeasureView.h"

#include "Camera/CameraCommon/ToolBarCamera.h"
#include "Camera/CameraCommon/CameraImage.h"
#include "Camera/CameraCommon/IndustryCamera.h"
#include "Camera/CameraCommon/CameraFinder.h"

#include "Nosepiece/NosepieceCommon/ToolbarNosepiece.h"
#include "Nosepiece/NosepieceCommon/LensManager.h"
#include "Nosepiece/NosepieceCommon/NosepieceInterface.h"
#include "Nosepiece/NosepieceCommon/NosepieceLibLoader.h"
#include "Nosepiece/NosepieceCommon/LensManageDialog.h"

#include "AutoFocus/FocusMeasureObject.h"

QT_BEGIN_NAMESPACE
namespace Ui {
class MainWindow;
}
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    enum ZOOM{ LEN0 = 0, LEN1 = 1, LEN2, LEN3, LEN4, LEN5 };
    MainWindow(QWidget *parent = nullptr);
    ~MainWindow();

protected slots:
    void showAbout();
    void onElementAdded(Qly::MeasureElement * p);
    void updateCameraImage(const uint8_t *imgBuf, int stride,  QSize size, AVPixelFormat format);
    void updateCameraFrame(Qly::VideoFrame frame);
    void updateMousePos(const QPointF &p);
    void onTimeoutFrameRateCounter();//用来更新真实的 FrameRate
    void onLensChanged(int oldLens, int newLens);
    void updateLensInfo();
    void updateFocusMeasure(QImage &image, double score);

    void toggleCameraState();
    void changePassword();
    void setClibMode(bool on);
    void showCalibDialog();
    void showLensManageDialog();

    /// 图像处理相关的函数
    void colorImageToGray();
    void redChannel();
    void blueChannel();
    void greenChannel();
    void rotateOrFlip();
protected slots:
    void clearAllMark();

protected:
    void closeEvent(QCloseEvent *event) Q_DECL_OVERRIDE;
    void keyPressEvent(QKeyEvent *event) Q_DECL_OVERRIDE;
    void setDefaultAppInfo();

    void setupMeasureView(QSettings &settings);
    void setupLensManager(QSettings &settings);
    void setupCamera(QSettings &settings);

    void setupStatusBar();
    void setupNosepiece(QSettings &settings);
    void setupImageProcessing();
    void savePassword(QSettings &settings);

    bool isSafeToClose();

    QString configFilePath();

    bool loadImage(QImage &image);
    bool loadImageFromFile(QString fileName);

private slots:
    void loadImage();
    void saveImageWithMark();
    void saveImageWithoutMark();
    void openDefaultCamera();
    void closeCamera();
protected:
    QString m_saveImagePath;
    QString m_loadImagePath;

    /// 显示相关
    QScrollArea *m_pScrollArea = nullptr;
    Qly::MeasureView *m_pMeasureView = nullptr;
    Qly::ToolBarMeasure *m_pToolbarMeasure = nullptr;
    Qly::ToolBarZoom *m_pToolbarZoom = nullptr;
    Qly::ToolBarProperty *m_pToolbarProperty = nullptr;

    Qly::ToolBarSemiAutoMeasure *m_pToolbarSemiMeasure = nullptr;
    Qly::SemiAutoMeasure *m_pSemiAutoMeasure = nullptr;
    Qly::DialogSemiAutoMeasure *m_pSemiAutoDialog = nullptr;

    QLabel * m_labelMousePosition = nullptr;
    QLabel * m_labelCameraName = nullptr;
    QLabel * m_labelCameraResolution = nullptr;
    QLabel * m_labelCameraFrameCount = nullptr;
    QLabel * m_labelCameraFrameRate = nullptr;
    QLabel * m_labelCameraPixelFormat = nullptr;
    QLabel * m_labelFocusMeasure = nullptr;
    QTimer * m_timerStatusBar = nullptr;

    /// 物镜相关
    Qly::ToolBarNosepiece *m_toolbarNosepiece = nullptr;
    Qly::NosepieceInterface * m_pNosepiece = nullptr;
    //Qly::NosepieceOUMIT *m_pNosepieceOUMIT = nullptr;
//    Qly::ToolBarLens * m_toolbarLens = nullptr;
    Qly::LensManageDialog * m_lensDialog = nullptr;
    Qly::LensManager *m_pLensManager = nullptr;

    Qly::NosepieceLibLoader * pNosepieceLib = nullptr;

    /// 相机相关
    Qly::ToolBarCamera *m_toolbarCamera = nullptr;
    Qly::CameraImage * m_cameraImage = nullptr;
    Qly::IndustryCamera * m_pCamera = nullptr;
    Qly::CameraFinder * m_cameraFind = nullptr;

    Qly::FocusMeasureObject m_focusMeasure;

    AboutDialog *m_aboutDialog = nullptr;

    QString m_password;

    bool m_saveImage;

    QString m_settingsFilePath;

    /// 以下是公司信息，在派生类里会设置这些内容
    QString m_appName;
    QString m_companyName;
    QString m_author;
    QString m_website;
    QString m_email;
    QString m_version;
    QString m_phoneNumber;
    QIcon m_appIcon;
private:
    Ui::MainWindow *ui;
};
#endif // MAINWINDOW_H
